package com.ifyyf.controller;

import com.ifyyf.pojo.Dept;
import com.ifyyf.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @Author if
 * @Description: What is it
 * @Date 2021-08-24 下午 12:13
 */
@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping("/get/{deptno}")
    //指定当前方法熔断的解决方法名称
    @HystrixCommand(fallbackMethod = "hystrixGet")
    public Dept get(@PathVariable("deptno") Long deptno){
        Dept dept = deptService.getById(deptno);
        if(dept==null){
            throw new NullPointerException("不存在id为 "+deptno+" 的数据");
        }
        return dept;
    }

    public Dept hystrixGet(Long deptno){
        return new Dept()
                .setDeptno(deptno)
                .setDname("不存在id为 "+deptno+" 的数据")
                .setSource("no this database in mysql");
    }

}
